<?php

/*
 * Override theme_links
 * 
 * @return 
 * @param $breadcrumb Object
 */
function phptemplate_links($links, $attributes = array("class" => "links")) {

	global $language;
	$output = "";

	if (count($links) > 0) {
    	$output = "<ul". drupal_attributes($attributes) .">";

		$num_links = count($links);
		$i = 1;

		foreach ($links as $key => $link) {
			$class = $key;

			if ($i == 1) {
				$class .= " first";
			}

			if ($i == $num_links) {
				$class .= " last";
			}

			if (isset($link["href"]) && ($link["href"] == $_GET["q"] || ($link["href"] == "<front>" && drupal_is_front_page()))
				&& (empty($link["language"]) || $link["language"]->language == $language->language)) {
				$class .= " active";
			}

			$output .= "<li". drupal_attributes(array("class" => $class)) .">";

			$link["attributes"] = array("title" => $link["title"], "class" => "internal");
			if (isset($link["href"])) {
				$output .= l($link["title"], $link["href"], $link);
			} else if (!empty($link["title"])) {
				if (empty($link["html"])) {
					$link["title"] = check_plain($link["title"]);
				}
				$span_attributes = "";
				if (isset($link["attributes"])) {
					$span_attributes = drupal_attributes($link["attributes"]);
				}
				$output .= "<span". $span_attributes .">". $link["title"] ."</span>";
			}

			$i++;
			$output .= "</li>\n";
		}
		$output .= "</ul>";
	}

	$output = preg_replace("/menu-[0-9]+\s+/u", "", $output);

	return $output;
}

/*
 * Override theme_breadcrumb
 * 
 * @return string
 * @param $breadcrumb Array
 */

function phptemplate_breadcrumb($breadcrumb) {

	global $base_path;

	if (!empty($breadcrumb)) {
		$links = array();
		foreach($breadcrumb as $key => $link) {
			$title = preg_replace("/<a href=\".+\">/u", "", $link);
			$title = preg_replace("/<\/a>/u", "", $title);

			$href = preg_replace("/<a href=\"/u", "", $link);
			$href = preg_replace("/\">.+<\/a>/u", "", $href);

			$pathLink  = "";
			if ($href == $base_path) {
				$pathLink .= "<a title=\"".$title."\" href=\"".$href."\" class=\"home\">".$title."</a>";
			} else {
				$pathLink .= "<a title=\"".$title."\" href=\"".$href."\" class=\"internal\">".$title."</a>";
			}
			array_push($links, $pathLink);
		}
		return "<p class=\"topicPath\">". implode(" &raquo; ", $links)."</p>";
	}
}





function phptemplate_imagemenu_item($item, $has_children, $prefix = '', $layout = 'vertical', $menu = '') {
//	$class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
	$class = (url($item["path"]) == base_path()) ? "home" : "internal";

	if (!empty($item['path'])) {
	  	$output  = "<li>";
//	  	$output  = "<li class=\"".$class."\">";
		$output .= "<a title=\"".$item["title"]."\" href=\"".url($item["path"])."\" class=\"".$class."\">";
		$output .= "<img src=\"".base_path().$item["imagepath"]."\" alt=\"".$item["alt"]."\" /></a>".$menu."</li>\n";
	} else {
	  	$output  = "<li>";
//	  	$output  = "<li class=\"delimiter\">";
		$output .= "<a title=\"".$item["title"]."\" href=\"".url($item["path"])."\" class=\"".$class."\">";
		$output .= "<img src=\"".base_path().$item['imagepath']."\" alt=\"".$item["alt"]."\" /></a>".$menu."</li>\n";
	}
	return $output;
}


function phptemplate_imagemenu_tree($tree) {  
  $output = "<ul class=\"navigation\">".$tree."</ul>";
  return $output;
}




function phptemplate_img_assist_inline($node, $size, $attributes) {
	$caption = '';
	if ($attributes['title'] && $attributes['desc']) {
		$caption = '<strong>'. $attributes['title'] .': </strong>'. $attributes['desc'];
	} elseif ($attributes['title']) {
		$caption = '<strong>'. $attributes['title'] .'</strong>';
	} elseif ($attributes['desc']) {
		$caption = $attributes['desc'];
	}

	$node->title = strip_tags($caption);
	$img_tag = img_assist_display($node, $size);
  
	// Always define an alignment class, even if it is 'none'.
//	$output = '<span class="inline inline-'. $attributes['align'] .'">';

	$link = $attributes['link'];
	$url  = '';

	// Backwards compatibility: Also parse link/url in the format link=url,foo.
	if (strpos($link, ',') !== FALSE) {
		list($link, $url) = explode(',', $link, 2);
	} elseif (isset($attributes['url'])) {
		$url = $attributes['url'];
	}

	if ($link == 'node') {
		$output .= l($img_tag, 'node/'. $node->nid, array('html' => TRUE));
	} elseif ($link == 'popup') {
	    $popup_size = variable_get('img_assist_popup_label', IMAGE_PREVIEW);
	    $info       = image_get_info(file_create_path($node->images[$popup_size]));
	    $width      = $info['width'];
	    $height     = $info['height'];
	    $popup_url  = file_create_url($node->images[variable_get('img_assist_popup_label', IMAGE_PREVIEW)]);
	    $output .= l($img_tag, $popup_url, array('attributes' => array('onclick' => "launch_popup({$node->nid}, {$width}, {$height}); return false;", 'target' => '_blank'), 'html' =>TRUE));
	} elseif ($link == 'url') {
		$output .= l($img_tag, $url, array('html' => TRUE));
	} else {
		$output .= $img_tag;
	}
  
	if ($caption) {
    	if ($attributes['align'] != 'center') {
    		$info = image_get_info(file_create_path($node->images[$size['key']]));
	    	// Reduce the caption width slightly so the variable width of the text
	    	// doesn't ever exceed image width.
	    	$width = $info['width'] - 2;
//	    	$output .= '<span class="caption" style="width: '. $width .'px;">'. $caption .'</span>';
		} else {
//			$output .= '<span class="caption">'. $caption .'</span>';
		}
	}
//	$output .= '</span>';
	return $output;
}


/*
 * Override theme_blocks
 * 
 * @return string
 * @param $region
 */
/*
function phptemplate_blocks($region) {
  
	$output = "";

	if ($region != "header") {
		if ($list = block_list($region)) {
			foreach ($list as $key => $block) {
	    		$output .= theme("block", $block);
			}
		}
	} else {
		if ($list = block_list($region)) {
			foreach ($list as $key => $block) {

	    		$block = theme("block", $block);
				$block = preg_replace("/^<div class=\"block.+\">/u", "", $block);
				$block = preg_replace("/$<\/div>/u", "", $block);

	    		$output .= $block;
			}
		}
	}
	$output .= drupal_get_content($region);

	return $output;
}
*/